package com.example.zhanghao.utils;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.nio.file.Paths;
import java.util.Collections;

/**
 * mybatis-plus代码自动生成器
 */
public class MyAutoGenerator {

    // 数据库链接参数
    private static String UserName = "root";
    private static String PassWord = "Simin168a";
    private static String Url = "jdbc:mysql://127.0.0.1:3306/zhanghao?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";

    public static void main(String[] args) {
        FastAutoGenerator.create(Url, UserName, PassWord)
                .globalConfig(builder -> builder
                        .author("wenJie")
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
                        .commentDate("yyyy-MM-dd")
                        .disableOpenDir()//生成后打开文件夹
                )
                .packageConfig(builder -> builder
                        .parent("com.example.zhanghao")
                        .entity("Entity")
                        .mapper("mapper")
                        .service("services")
                        .serviceImpl("services.impl")
                        .xml("mapper.xml")
                        .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/src/main/resources/mapper"))
                )
                .strategyConfig(builder -> {
                            builder
                            .addInclude("workmessage")
                            .entityBuilder()
                            .enableLombok()
                            .addTableFills(new Column("create_time", FieldFill.INSERT))
                            .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                            .controllerBuilder()
                            .disable();

                            // !!! 核心配置：设置为 true 即覆盖所有已生成的文件 !!!
//                            builder.controllerBuilder().fileOverride();
                            builder.entityBuilder().fileOverride();
//                            builder.mapperBuilder().fileOverride();
//                            builder.serviceBuilder().fileOverride();
                        }
                )
                .templateEngine(new FreemarkerTemplateEngine())

                .execute();
    }
}